import time
import types
import inspect
import sys

from qturboframe.lib.report.log import logger


class TimeMeta:
    TimeMetaSwitch = False

    CalledFunctions =[]
    CalledFunctionsTimeDict = {}
    def __getattribute__(self, item):
        start_time = time.time()
        func = super().__getattribute__(item)

        if "__init__" in item:
            pass

        if type(func)==types.MethodType and TimeMeta.TimeMetaSwitch:
            def helper(*args, **kwargs):
                helper.calls += 1
                ret = func(*args, **kwargs)

                calledfun = func.__module__+":"+func.__name__
                calledtimes = 1
                if calledfun in TimeMeta.CalledFunctions:
                    calledtimes = TimeMeta.CalledFunctionsTimeDict[calledfun] + 1
                    TimeMeta.CalledFunctionsTimeDict[calledfun] = calledtimes
                else:
                    TimeMeta.CalledFunctions.append(calledfun)
                    TimeMeta.CalledFunctionsTimeDict[calledfun] = calledtimes
                logger.debug("{}方法执行时间为：{}---->被调用：{}次".format(calledfun,time.time()-start_time,calledtimes))
                return ret

            helper.calls = 0
            helper.__name__ = func.__name__

            return helper
        else:
            return func